﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CNotebook.Inventory
{
    public class StockRequestManager:ConnectionManager
    {
        private SqlCommand oCommand = new SqlCommand();

        public void AddRequest(StockRequestUnit oUnit)
        {
            oCommand.Connection = this.Connection;
            oCommand.CommandText = "INSERT INTO OSTCKREQ (DocNumber, CardCode, CardName, Reference, Remarks, DateNeeded, DateCreated, CreatedBy, DateModified, ModifiedBy) VALUES (@DocNumber, @CardCode, @CardName, @Reference, @Remarks, @DateNeeded, @DateCreated, @CreatedBy, @DateModified, @ModifiedBy)";
            
            oCommand.Parameters.Add(new SqlParameter("@DocNumber", oUnit.DocNumber));
            oCommand.Parameters.Add(new SqlParameter("@CardCode", oUnit.CardCode));
            oCommand.Parameters.Add(new SqlParameter("@CardName", oUnit.CardName));
            oCommand.Parameters.Add(new SqlParameter("@Reference", oUnit.Reference));
            oCommand.Parameters.Add(new SqlParameter("@Remarks", oUnit.Remarks));
            oCommand.Parameters.Add(new SqlParameter("@DateNeeded", oUnit.DateNeeded));
            oCommand.Parameters.Add(new SqlParameter("@DateCreated", oUnit.DateCreated));
            oCommand.Parameters.Add(new SqlParameter("@CreatedBy", oUnit.CreatedBy));
            oCommand.Parameters.Add(new SqlParameter("@DateModified", oUnit.DateModified));
            oCommand.Parameters.Add(new SqlParameter("@ModifiedBy", oUnit.ModifiedBy));

            oCommand.ExecuteNonQuery();

            foreach (DataRow oRow in oUnit.LineItems.Rows)
            {
                oCommand = new SqlCommand();
                oCommand.Connection = this.Connection;
                oCommand.CommandText = "INSERT INTO STCKREQ1 (DocNum, ItemCode, ItemName, UoM, Qty) VALUES (@DocNum, @ItemCode, @ItemName, @UoM, @Qty)";
                oCommand.Parameters.Add(new SqlParameter("@DocNum", oUnit.DocNumber));
                oCommand.Parameters.Add(new SqlParameter("@ItemCode", oRow["ItemCode"].ToString()));
                oCommand.Parameters.Add(new SqlParameter("@ItemName", oRow["ItemName"].ToString()));
                oCommand.Parameters.Add(new SqlParameter("@UoM", oRow["UoM"].ToString()));
                oCommand.Parameters.Add(new SqlParameter("@Qty", Convert.ToDouble(oRow["Qty"].ToString())));

                oCommand.ExecuteNonQuery();
            }
        }
    }
}
